(function() {
  if (!window || !document) {
    return;
  }
  var metaEl = document.querySelector('meta[name="viewport"]'),
    metaCtt = metaEl ? metaEl.content : '',
    matchScale = metaCtt.match(/initial\-scale=([\d\.]+)/),
    matchWidth = metaCtt.match(/width=([^,\s]+)/);
  if (metaEl && !matchScale && matchWidth && matchWidth[1] != 'device-width') {
    var width = parseInt(matchWidth[1]),
      iw = window.innerWidth || width,
      ow = window.outerWidth || iw,
      sw = window.screen.width || iw,
      saw = window.screen.availWidth || iw,
      ish = window.screen.height || ih,
      sah = window.screen.availHeight || ih,
      w = Math.min(iw, ow, sw, saw, ish, sah),
      scale = w / width;
    if (scale < 1) {
      metaEl.content +=
        ',initial-scale=' +
        scale +
        ',maximum-scale=' +
        scale +
        ', minimum-scale=' +
        scale +
        ',user-scalable=no';
    }
  }
})();
